package com.gjs.user.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gjs.common.enums.entity.StatusEnum;
import com.gjs.common.exception.BizException;
import com.gjs.common.manager.SecurityOauthUtil;
import com.gjs.common.pojo.dto.user.UserInfoDTO;
import com.gjs.common.pojo.dto.user.UserQueryDTO;
import com.gjs.common.pojo.dto.user.UserRegisterDTO;
import com.gjs.user.mapper.UserMapper;
import com.gjs.user.pojo.entity.User;
import com.gjs.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.UUID;


@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SecurityOauthUtil oauthUtil;

    @Override
    public User getUserBySecurity() {
        UserQueryDTO query = new UserQueryDTO();
        query.setUserSn(oauthUtil.getUserSnByJwt());
        User user = this.getOne(this.buildWrapper(query));
        Assert.notNull(user,"用户不存在");

        return user;
    }

    @Override
    public UserInfoDTO userRegister(UserRegisterDTO userRegisterDto){
        // 校验登录账号或者手机号是否已注册过
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .eq(User::getUserAccount, userRegisterDto.getUserAccount())
                .or()
                .eq(User::getMobile, userRegisterDto.getMobile());
        User check = this.getOne(wrapper);
        if (check != null) {
            throw new BizException("账号已注册");
        }

        User user = BeanUtil.copyProperties(userRegisterDto,User.class);
        // 生成用户唯一序列号
        user.setUserSn(UUID.randomUUID().toString());
        // 用户新注册默认是可用状态
        user.setStatus(StatusEnum.ENABLE);
        // 加密用户密码
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        // 执行写入数据库
        boolean result = this.save(user);
        if(!result){
            throw new BizException("账号注册失败，请重试");
        }

        return BeanUtil.copyProperties(user, UserInfoDTO.class);
    }

    @Override
    public boolean resetPassword(String mobile, String password) {
        UserQueryDTO queryDTO = new UserQueryDTO();
        queryDTO.setMobile(mobile);
        User user = this.getOne(buildWrapper(queryDTO));
        Assert.notNull(user,"用户不存在");
        // 设置加密后的密码
        user.setPassword(passwordEncoder.encode(password));
        // 执行更新操作
        return this.updateById(user);
    }

    /**
     * 组装用户查询条件
     */
    @Override
    public Wrapper<User> buildWrapper(UserQueryDTO queryDTO) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!StrUtil.isEmpty(queryDTO.getUserSn()), User::getUserSn, queryDTO.getUserSn());
        wrapper.in(queryDTO.getUserSnSet() != null && !queryDTO.getUserSnSet().isEmpty(), User::getUserSn, queryDTO.getUserSnSet());
        wrapper.eq(!StrUtil.isEmpty(queryDTO.getMobile()), User::getMobile, queryDTO.getMobile());
        wrapper.like(!StrUtil.isEmpty(queryDTO.getNickname()), User::getNickname, queryDTO.getNickname());
        wrapper.eq(!StrUtil.isEmpty(queryDTO.getUserAccount()), User::getUserAccount, queryDTO.getUserAccount());

        return wrapper;
    }
}
